home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 11 / Info-Mac_XI_Disc_1.cdr_ / Info-Mac XI Disc 1.cdr / Programs / Science & Math / MacEspresso 1.0 / espresso / solution.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-26  |  1.6 KB  |  106 lines  |  [TEXT/R*ch]

  1. #include "mincov_int.h"
  2.  
  3.  
  4. solution_t *
  5. solution_alloc()
  6. {
  7.     solution_t *sol;
  8.  
  9.     sol = ALLOC(solution_t, 1);
  10.     sol->cost = 0;
  11.     sol->row = sm_row_alloc();
  12.     return sol;
  13. }
  14.  
  15.  
  16. void
  17. solution_free(sol)
  18. solution_t *sol;
  19. {
  20.     sm_row_free(sol->row);
  21.     FREE(sol);
  22. }
  23.  
  24.  
  25. solution_t *
  26. solution_dup(sol)
  27. solution_t *sol;
  28. {
  29.     solution_t *new_sol;
  30.  
  31.     new_sol = ALLOC(solution_t, 1);
  32.     new_sol->cost = sol->cost;
  33.     new_sol->row = sm_row_dup(sol->row);
  34.     return new_sol;
  35. }
  36.  
  37.  
  38. void 
  39. solution_add(sol, weight, col)
  40. solution_t *sol;
  41. int *weight;
  42. int col;
  43. {
  44.     (void) sm_row_insert(sol->row, col);
  45.     sol->cost += WEIGHT(weight, col);
  46. }
  47.  
  48.  
  49. void 
  50. solution_accept(sol, A, weight, col)
  51. solution_t *sol;
  52. sm_matrix *A;
  53. int *weight;
  54. int col;
  55. {
  56.     register sm_element *p, *pnext;
  57.     sm_col *pcol;
  58.  
  59.     solution_add(sol, weight, col);
  60.  
  61.     /* delete rows covered by this column */
  62.     pcol = sm_get_col(A, col);
  63.     for(p = pcol->first_row; p != 0; p = pnext) {
  64.     pnext = p->next_row;        /* grab it before it disappears */
  65.     sm_delrow(A, p->row_num);
  66.     }
  67. }
  68.  
  69.  
  70. /* ARGSUSED */
  71. void 
  72. solution_reject(sol, A, weight, col)
  73. solution_t *sol;
  74. sm_matrix *A;
  75. int *weight;
  76. int col;
  77. {
  78.     sm_delcol(A, col);
  79. }
  80.  
  81.  
  82. solution_t *
  83. solution_choose_best(best1, best2)
  84. solution_t *best1, *best2;
  85. {
  86.     if (best1 != NIL(solution_t)) {
  87.     if (best2 != NIL(solution_t)) {
  88.         if (best1->cost <= best2->cost) {
  89.         solution_free(best2);
  90.         return best1;
  91.         } else {
  92.         solution_free(best1);
  93.         return best2;
  94.         }
  95.     } else {
  96.         return best1;
  97.     }
  98.     } else {
  99.     if (best2 != NIL(solution_t)) {
  100.         return best2;
  101.     } else {
  102.         return NIL(solution_t);
  103.     }
  104.     }
  105. }
  106.